package com.erp.module.yuliaofreight

import android.content.Context
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.YuliaoGiFreightBean
import com.erp.pull.BasePullP
import com.erp.util.JsonUtil
import org.json.JSONException
import org.json.JSONObject

class IYuliaoGiFreightP(var V: YuliaoFreightConteract.YuliaoGiFreightView, var  context : Context, var list :MutableList<YuliaoGiFreightBean>) : BasePullP(), YuliaoFreightConteract.YuliaoGiFreight  {
    var md =YuliaoFreightModel()
    var length:String = ""
    var width:String = ""
    var thickness:String = ""
    var color:String = ""
    var name:String = ""
    var mc_2_name_mh:String = ""
    var yfs_id = ""
    init {
        PullV = V
    }
    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        md.getYuliao(context,page,pagesize,onUserDataInterface,yfs_id,length,width,thickness,mc_2_name_mh,color,name)
    }

    override fun jsondata(data: String?) {
        if (data==null){
            return
        }
        var total = "";
        try {
            val j = JSONObject(data)
            val jo = JsonUtil.getjovolue(j,"data")
            total = JsonUtil.getvolue(jo,"total");
            val js = JsonUtil.getjsvolue(jo,"data")
            for (i in 0 until js.length()) {
                val bean = YuliaoGiFreightBean()
                val jo = js.get(i) as JSONObject
                bean.freight_space_name = JsonUtil.getvolue(jo, "freight_space_name")
                bean.yfs_id = JsonUtil.getvolue(jo, "yfs_id")
                bean.yfsg_id = JsonUtil.getvolue(jo, "yfsg_id")
                bean.name = JsonUtil.getvolue(jo, "name")
                bean.code = JsonUtil.getvolue(jo, "code")
                bean.length = JsonUtil.getvolue(jo, "length")
                bean.width = JsonUtil.getvolue(jo, "width")
                bean.thickness = JsonUtil.getvolue(jo, "thickness")
                bean.color = JsonUtil.getvolue(jo, "color")
                bean.intime = JsonUtil.getvolue(jo, "intime")
                list.add(bean)
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        V.setTotal(total)
    }

    override fun listClean() {
        list.clear()
    }

    override fun fetchYuliao(yfsg_id: String, length: String,rucangcode:String) {
        md.fetchYuliao(context,yfsg_id,length,rucangcode,object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                V.outYuliaoChucangOk(data.toString())
            }

            override fun onNot(throwable: String?) {
                V.outYuliaoChucangOk(throwable.toString())
            }

        })
    }

    override fun outYuliao(yfsg_id: String,rucangcode:String) {
        md.outYuliao(context,yfsg_id,rucangcode,object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                V.outYuliaoChucangOk(data.toString())
            }

            override fun onNot(throwable: String?) {
                V.outYuliaoChucangOk(throwable.toString())
            }

        })
    }

    override fun getCangweiData() {
        md.getCangweiData(context,object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                JsonCangwei(data)
            }

            override fun onNot(throwable: String?) {
            }

        })
    }

    fun sousuo(yfs_id:String,mc_2_name_mh:String,length:String,width:String,thickness:String,color:String,name:String){
        this.yfs_id = yfs_id
        this.length = length.trim()
        this.width = width.trim()
        this.thickness = thickness.trim()
        this.mc_2_name_mh = mc_2_name_mh
        this.color = color.trim()
        this.name = name.trim()
    }

    fun JsonCangwei(data: String?){
        if (data==null){
            return
        }

        var cangweinames: MutableList<String> = ArrayList<String>()
        var cangweiids: MutableList<String> = ArrayList<String>()
        try {
            val j = JSONObject(data)
            val js = JsonUtil.getjsvolue(j,"data")
            for (i in 0 until js.length()) {
                val jo = js.get(i) as JSONObject
                cangweiids.add(JsonUtil.getvolue(jo, "ysf_id"))
                cangweinames.add(JsonUtil.getvolue(jo, "freight_space_name"))
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        V.getCangwei(cangweiids,cangweinames)
    }
}